package com.jtjrenren.android.taxi.driver.core.user.rsetpwd;

import android.view.View;
import android.widget.TextView;

import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.bean.DriverInfo;
import com.jtjrenren.android.taxi.driver.bean.api.CommonResult;
import com.jtjrenren.android.taxi.driver.manger.AppManger;

import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.library.utils.StringUtils;
import com.library.weiget.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/5/19.
 */
public class UserChangePwdActivity extends BaseActivity {
    @BindView(R.id.user_uppwd_opwd)
    ClearEditText et_oldpwd;
    @BindView(R.id.user_uppwd_npwd)
    ClearEditText et_newpwd;
    @BindView(R.id.user_uppwd_npwd2)
    ClearEditText et_newpwd2;


    private DriverInfo driver;

    @Override
    public int getLayoutID() {
        return R.layout.aty_user_changepwd;
    }

    @Override
    public void init() {
        driver = AppManger.getInstance().driver;

        ((TextView) findViewById(R.id.title_title)).setText("修改密码");
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @OnClick(R.id.user_uppwd_update)
    public void clickUpdate() {
        if (driver == null)
            return;
        resetPwd();
    }

    private void resetPwd() {
        String oldpwd = et_oldpwd.getText().toString().trim();
        String npwd = et_newpwd.getText().toString().trim();
        String npwd2 = et_newpwd2.getText().toString().trim();

        if (StringUtils.isEmpty(oldpwd)||StringUtils.isEmpty(npwd)){
            displayShort("请输入密码");
            return;
        }

        if (oldpwd.equals(npwd)) {
            displayShort(getString(R.string.mimaxiangtong));
            return;
        }

        if (npwd.length() < 6) {
            displayShort(getString(R.string.mimachagnduzuishaowei6wei));
            return;
        }

        if (!npwd.equals(npwd2)) {
            displayShort(getString(R.string.mimashurubuyizhi));
            return;
        }

        ApiWorks.upPwd(oldpwd, npwd, driver.getWorkNo(), new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                if (commonResult != null && commonResult.getCode() == 1) {
                    displayShort(getString(R.string.xiugaichenggong));
                    finish();
                } else
                    displayShort(getString(R.string.xiugaishibai));
            }
        });
    }
}
